package com.js.lib_common.mvp.presenter;


import com.google.gson.Gson;
import com.js.lib_common.base.BasePresenter;
import com.js.lib_common.http.ApiCallback;
import com.js.lib_common.mvp.model.CommonBean;
import com.js.lib_common.mvp.model.response.BindBean;
import com.js.lib_common.mvp.model.response.MachineIdInfoResponse;
import com.js.lib_common.mvp.model.response.MaterialIdInfoResponse;
import com.js.lib_common.mvp.model.response.ProdyctInfoResponse;
import com.js.lib_common.mvp.view.BindingView;

import java.util.List;


public class BindingPresenter extends BasePresenter<BindingView> {
    Gson gson=new Gson();
    public BindingPresenter(BindingView view) {
        attachView(view);
    }


    public void checkMachineId(String userId, final String machineId) {
        if (userId == null || userId.isEmpty()) {
            mvpView.onFailed("userId 为空");
            return;
        }
        mvpView.showLoading();
        addSubscription(apiStores.checkMachineId(userId, machineId),
                new ApiCallback<CommonBean<MachineIdInfoResponse>>() {
                    @Override
                    public void onSuccess(CommonBean<MachineIdInfoResponse> response) {
                        if (response.isSuccess()) {
                            BindBean bean=new BindBean();
                            bean.setQrcode(machineId);
                            bean.setType(BindBean.REQUEST_CODE_BIND_MACHINEID);
                            bean.setInfo("录入,整机码");
                            bean.setMachineIdInfo(response.getObj());
                            mvpView.onSuccess(bean);
                        } else {
                            mvpView.onFailed(String.format("%s", response.getMsg()));
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        mvpView.onFailed(String.format("%s", msg));
                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });

    }

    public void checkMaterialId(String userId, final String materialId) {
        if (userId == null || userId.isEmpty()) {
            mvpView.onFailed("userId 为空");
            return;
        }
        mvpView.showLoading();
        addSubscription(apiStores.checkMaterialId(userId, materialId),
                new ApiCallback<CommonBean<MaterialIdInfoResponse>>() {
                    @Override
                    public void onSuccess(CommonBean<MaterialIdInfoResponse> response) {
                        if (response.isSuccess()) {
                            BindBean bean=new BindBean();
                            bean.setQrcode(materialId);
                            bean.setType(BindBean.REQUEST_CODE_BIND_MATERIALID);
                            bean.setInfo("录入,物料码");
                            bean.setMaterialIdInfo(response.getObj());
                            mvpView.onSuccess(bean);
                        } else {
                            mvpView.onFailed(String.format("%s", response.getMsg()));
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        mvpView.onFailed(String.format("%s", msg));
                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });

    }

//    public void checkProcessCode(final int type,String userId,final String processCode) {
//        if (userId == null || userId.isEmpty()) {
//            mvpView.onFailed("userId 为空");
//            return;
//        }
//        mvpView.showLoading();
//        addSubscription(apiStores.checkProcessCode(userId,  processCode),
//                new ApiCallback<CommonBean<ProcessInfoInfoResponse>>() {
//                    @Override
//                    public void onSuccess(CommonBean<ProcessInfoInfoResponse> response) {
//                        if (response.isSuccess()) {
//                            BindBean bean=new BindBean();
//                            bean.setQrcode(processCode);
//                            bean.setType(type);
//                            bean.setInfo("录入,工序码");
//                            bean.setProcessInfoInfo(response.getObj());
//
////                            String json = (String) SPUtils.get(LibCommon.mContext, "defaultWork", "");
////                            if (json != null && !json.isEmpty()) {
////                                List<BindBean> list = gson.fromJson(json, new TypeToken<List<BindBean>>() {
////                                }.getType());
////                                if (list.contains(bean)){
////                                    bean.setType(BindBean.REQUEST_CODE_DEFAULT_WORK);
////                                }
////
////                            }
//                            mvpView.onSuccess(bean);
//
//                        } else {
//                            mvpView.onFailed(String.format("%s", response.getMsg()));
//                        }
//                    }
//
//                    @Override
//                    public void onFailure(String msg) {
//                        mvpView.onFailed(String.format("%s", msg));
//                    }
//
//                    @Override
//                    public void onFinish() {
//                        mvpView.hideLoading();
//                    }
//                });
//
//    }


    public void bind(String userId, String machineID, String materialIDs, String processCode,int bindType) {
        if (userId == null || userId.isEmpty()) {
            mvpView.onFailed("userId 为空");
            return;
        }
        mvpView.showLoading();
        addSubscription(apiStores.bind(userId, machineID, materialIDs, processCode,bindType),
                new ApiCallback<CommonBean>() {

                    @Override
                    public void onSuccess(CommonBean response) {
                        if (response.isSuccess()) {
                            mvpView.onBindSuccess();
                        } else {
                            mvpView.onFailed(response.getMsg());
                        }

                    }

                    @Override
                    public void onFailure(String msg) {
                        mvpView.onFailed(String.format("%s", msg));
                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }



    public void getAllProductInfo(String userId) {

        mvpView.showLoading();
        addSubscription(apiStores.getAllProductInfo(userId),
                new ApiCallback<CommonBean<List< ProdyctInfoResponse >>>() {

                    @Override
                    public void onSuccess(CommonBean<List<ProdyctInfoResponse>> response) {
                        if (response.isSuccess()) {
                            mvpView.getProductInfoSuccess(response.getObj());
                        } else {
                            mvpView.onFailed(response.getMsg());
                        }

                    }

                    @Override
                    public void onFailure(String msg) {
                        mvpView.onFailed(String.format("%s", msg));
                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }



}
